package com.ultrasoft.farmasoft.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Access;
import com.ultrasoft.farmasoft.domain.Defaultaccess;
import com.ultrasoft.farmasoft.domain.Option;
import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.Rol;
import com.ultrasoft.farmasoft.domain.Submenu;
import com.ultrasoft.farmasoft.domain.User;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.repository.AccessDao;
import com.ultrasoft.farmasoft.repository.DefaultAccessDao;
import com.ultrasoft.farmasoft.repository.MenuDao;
import com.ultrasoft.farmasoft.repository.OptionDao;
import com.ultrasoft.farmasoft.repository.PharmacyDao;
import com.ultrasoft.farmasoft.repository.RolDao;
import com.ultrasoft.farmasoft.repository.SubMenuDao;
import com.ultrasoft.farmasoft.repository.UserDao;
import com.ultrasoft.farmasoft.repository.UserPharmacyDao;
import com.ultrasoft.farmasoft.domain.Menu;

@Service
public class HumanResourcesServiceImpl implements HumanResourcesService {
	private AccessDao accessDao;
	private DefaultAccessDao defaultAccessDao;
	private OptionDao optionDao;
	private PharmacyDao pharmacyDao;
	private RolDao rolDao;
	private SubMenuDao subMenuDao;
	private MenuDao menuDao;
	private UserDao userDao;
	private UserPharmacyDao userPharmacyDao;
	
	@Autowired
    public HumanResourcesServiceImpl(UserPharmacyDao userPharmacyDao,UserDao userDao,MenuDao menuDao,SubMenuDao subMenuDao,OptionDao optionDao,RolDao rolDao,PharmacyDao pharmacyDao,DefaultAccessDao defaultAccessDao,AccessDao accessDao){//,RolRepository rolRepository) {
        this.accessDao					= accessDao;
        this.defaultAccessDao 			= defaultAccessDao;
        this.pharmacyDao 				= pharmacyDao;
        this.rolDao 					= rolDao;
        this.menuDao 					= menuDao;
        this.subMenuDao 				= subMenuDao;
        this.optionDao 					= optionDao;
        this.userDao 					= userDao;
        this.userPharmacyDao 			= userPharmacyDao;
    }
	// start Menu
	@Transactional(readOnly = true)
	public List<Menu> getMenuList(){
		return menuDao.getMenuList();
	}
	// end Menu
	
	
	// start Access
    @Transactional(readOnly = true)	
	public List<Access> getAccessList(){
    	return accessDao.getAccessList();
    }
    @Transactional(readOnly = true)	
	public List<Access> getAccessList(Integer userId){
    	return accessDao.getAccessList(userId);
	}
    @Transactional(readOnly = true)	
	public List<Access> getAccessListByPharmacyId(Integer pharmacyId){
		return accessDao.getAccessListByPharmacyId(pharmacyId);
	}
    @Transactional(readOnly = true)	
	public List<Access> getAccessList(Integer userId,Integer pharmacyId){
    	return accessDao.getAccessList(userId, pharmacyId);
	}
    @Transactional(readOnly = true)	
	public List<Access> getAccessList(Integer userId,Integer pharmacyId, short menuId,	short submenuId){
		return accessDao.getAccessList(userId, pharmacyId, menuId, submenuId);
	}
    @Transactional(readOnly = true)	
	public Access searchAccessByPharmacyIdByUserIdByMenuIdBySubmenuIdByOptionId(Integer pharmacyId,Integer userId,short menuId, short submenuId, short optionId){
    	return accessDao.findByPharmacyIdByUserIdByMenuIdBySubmenuIdByOptionId(pharmacyId, userId, menuId, submenuId, optionId);
    }	   
    @Transactional
    public void removeAccess(Access access){
		accessDao.remove(access);
	}
    @Transactional
	public void removeAccessByUserByPharmacyId(Integer userId,Integer pharmacyId){
    	List<Access> listAcces= accessDao.getAccessList(userId, pharmacyId);
        for (Access access : listAcces) {
			accessDao.remove(access);
		}
	}
	// end Access

    // start Defaultaccess
    @Transactional(readOnly = true)		
	public List<Defaultaccess> getDefaultAccessList(){
		return defaultAccessDao.getDefaultAccessList();
	}
    @Transactional(readOnly = true)	
	public List<Defaultaccess> getDefaultAccessList(Integer rolId){
		return defaultAccessDao.getDefaultAccessList(rolId);
	}
    @Transactional(readOnly = true)	
	public List<Defaultaccess> getDefaultAccessList(Integer rolId,short menuId, short submenuId){
		return defaultAccessDao.getDefaultAccessList(rolId, menuId, submenuId);		
	}
    @Transactional(readOnly = true)
    public Defaultaccess searchDefaultAccessByRolIdByMenuIdBySubMenuIdByOptionId(Integer rolId,short menuId, short submenuId,short optionId){
    	return defaultAccessDao.findByRolIdByMenuIdBySubMenuIdByOptionId(rolId, menuId, submenuId, optionId);
    }   
    // end Defaultaccess

    // start Option
    @Transactional(readOnly = true)	
    public List<Option> getOptionList(){
    	return optionDao.getOptionList();
    }
    @Transactional(readOnly = true)
	public List<Option> getOptionList(short menuId){
    	return optionDao.getOptionList(menuId);
    }
    @Transactional(readOnly = true)	
	public List<Option> getOptionList(short menuId,short submenuId){
		return optionDao.getOptionList(menuId, submenuId);
	}
    @Transactional(readOnly = true)	
    public Option searchOptionByMenuIdBySubmenuIdByOptionId(short menuId, short submenuId,short optionId){
    	return optionDao.findByMenuIdBySubmenuIdByOptionId(menuId, submenuId, optionId);
    }      
    @Transactional
    public void addOption(Option option){
    	optionDao.add(option);
    }
    // end Option

    // start Pharmacy   
    @Transactional(readOnly = true)	
    public List<Pharmacy> getListPharmacy(){
    	return pharmacyDao.getListPharmacy();
    }
    @Transactional(readOnly = true)	
    public Pharmacy searchPharmacyByPharmacyId(Integer pharmacyId){
    	return pharmacyDao.findByPharmacyId(pharmacyId);
    }
    @Transactional
    public void updatePharmacy(Pharmacy pharmacy){
    	pharmacyDao.update(pharmacy);
    }
    @Transactional
    public void removePharmacy(Pharmacy pharmacy){
    	pharmacyDao.remove(pharmacy);
    }    
    @Transactional
    public void addPharmacy(Pharmacy pharmacy){
    	pharmacyDao.add(pharmacy);
    }
    // end Pharmacy    
    
    // start Rol
    @Transactional(readOnly = true)	
    public List<Rol> getRolList(){
    	return rolDao.getRolList();
    }
    @Transactional(readOnly = true)
    public Rol searchRolByRolId(Short idRol){
    	return rolDao.findByRolId(idRol);
    }
    // end Rol
    
    // start Submenu
    @Transactional(readOnly = true)
    public List<Submenu> getSubMenuList(){
    	return subMenuDao.getSubMenuList();
    }
    @Transactional(readOnly = true)
	public List<Submenu> getSubMenuList(short menuId){
		return subMenuDao.getSubMenuList(menuId);
	}
    @Transactional(readOnly = true)
    public Submenu searchSubMenuByMenuIdBySubmenuId(short menuId, short submenuId){
    	return subMenuDao.findByMenuIdBySubmenuId(menuId, submenuId);
    }      
    // end Submenu
    
    // start User
    @Transactional(readOnly = true)    
	public List<User> getUserList(){
		return userDao.getUserList();
	}
    @Transactional(readOnly = true)    
	public List<User> getUserList(Integer rolId){
		return userDao.getUserList(rolId);
	}
    @Transactional(readOnly = true)  
	public List<User> getUserListExcept(Integer userId){
		return userDao.getUserListExcept(userId);
	}
    @Transactional(readOnly = true)  
	public List<User> getUserList(String login){
		return userDao.getUserList(login);
	}
    @Transactional(readOnly = true)    
    public User searchUserByUserId(Integer userId){
    	return userDao.findByUserId(userId);
    }
    @Transactional(readOnly = true)    
    public User searchUserByLoginByPassword(String login, String password){
    	return userDao.findByLoginByPassword(login, password);
    }
    @Transactional    
    public void updateUser(User user){
    	userDao.update(user);
    }
    @Transactional
    public void removeUser(User user){
    	userDao.remove(user);
    }    
    @Transactional
    public void addUser(User user){
    	userDao.add(user);
    }   
    // end User
	
    // start Userpharmacy
    @Transactional(readOnly = true)        
    public List<Userpharmacy> getUserPharmacyList(){
    	return userPharmacyDao.getUserPharmacyList();
    }
    @Transactional(readOnly = true)
	public List<Userpharmacy> getUserPharmacyList(boolean active){
		return userPharmacyDao.getUserPharmacyList(active);
	}
    @Transactional(readOnly = true)   
	public List<Userpharmacy> getUserPharmacyList(Integer userId){
		return userPharmacyDao.getUserPharmacyList(userId);
	}
    @Transactional(readOnly = true)    
	public List<Userpharmacy> getUserPharmacyList(Integer userId,boolean active){
		return userPharmacyDao.getUserPharmacyList(userId, active);
	}
    
	@Transactional(readOnly = true)    
	public List<Userpharmacy> getListUserPharmacy(String login, String password){
		return userPharmacyDao.getListUserPharmacy(login, password);
	}
	
	@Transactional(readOnly = true)    
	public List<Userpharmacy> getListUserPharmacy(Integer pharmacyId,boolean active){
		return userPharmacyDao.getListUserPharmacy(pharmacyId, active);
	}
    
    @Transactional(readOnly = true)  
	public Userpharmacy searchUserPharmacyByPharmacyIdByUserId(Integer pharmacyId,Integer userId){
    	return userPharmacyDao.findByPharmacyIdByUserId(pharmacyId, userId);
    }
    
    @Transactional
    public void updateUserPharmacy(Userpharmacy userPharmacy){
    	userPharmacyDao.update(userPharmacy);
    }
    
    @Transactional
    public void addUserPharmacy(Userpharmacy userPharmacy){
    	userPharmacyDao.add(userPharmacy);
    }
    // end Userpharmacy
    
    // start extras
    @Transactional(readOnly = true)
   	public List<Menu> getMenuListWithSubmenuAndOptions(){
       	List<Menu> listMenu=menuDao.getMenuList();
       	List<Menu> listMenutemp=new ArrayList<Menu>();
       	for (Menu menu : listMenu) {
       		List<Submenu> listSubMenu=subMenuDao.getSubMenuList(menu.getMenuId());
			List<Submenu> listSubMenutemp=new ArrayList<Submenu>();
			for (Submenu submenu : listSubMenu) {
				List<Option> listOptions=optionDao.getOptionList(submenu.getSubmenuPK().getMenuId(),submenu.getSubmenuPK().getSubmenuId());
				submenu.setOptionCollection(listOptions);
				listSubMenutemp.add(submenu);
			}
			menu.setSubmenuCollection(listSubMenutemp);
			listMenutemp.add(menu);
		}
       	return listMenutemp;
    }
    @Transactional
   	public void addAccessByRequestByUserPharmacy(HttpServletRequest request,Userpharmacy userPharmacy){
        try {
        	String[] listOption	= request.getParameterValues("check_option");
			String[] listSubmenu= request.getParameterValues("check_submenu");
			if(listOption != null){
				for (int i = 0; i < listOption.length; i++) {
					String[] id = listOption[i].split("_");
					if(optionDao.findByMenuIdBySubmenuIdByOptionId(Short.parseShort(id[0]), Short.parseShort(id[1]), Short.parseShort(id[2])) !=null)
						if(accessDao.findByPharmacyIdByUserIdByMenuIdBySubmenuIdByOptionId(userPharmacy.getUserpharmacyPK().getPharmacyId(),userPharmacy.getUserpharmacyPK().getUserId(),Short.parseShort(id[0]),Short.parseShort(id[1]), Short.parseShort(id[2])) == null)				
							accessDao.add(new Access(new Option(Short.parseShort(id[0]), Short.parseShort(id[1]), Short.parseShort(id[2])), userPharmacy));
				}
			}
			if(listSubmenu != null){
				for (int i = 0; i < listSubmenu.length; i++) {
					String[] id = listSubmenu[i].split("_");
					if(optionDao.findByMenuIdBySubmenuIdByOptionId(Short.parseShort(id[0]), Short.parseShort(id[1]),(short)1) !=null)
						if(accessDao.findByPharmacyIdByUserIdByMenuIdBySubmenuIdByOptionId(userPharmacy.getUserpharmacyPK().getPharmacyId(),userPharmacy.getUserpharmacyPK().getUserId(),Short.parseShort(id[0]),Short.parseShort(id[1]),(short)1) == null)				
							accessDao.add(new Access(new Option(Short.parseShort(id[0]), Short.parseShort(id[1]),(short)1),userPharmacy));
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
    // end extras    
}
